package com.example.dryclean_admin.service.impl;

import com.example.dryclean_admin.entity.Detergent;
import com.example.dryclean_admin.entity.Orderlist;
import com.example.dryclean_admin.entity.ProgressStage;
import com.example.dryclean_admin.mapper.OrderlistMapper;
import com.example.dryclean_admin.service.IDetergentService;
import com.example.dryclean_admin.service.IOrderlistService;
import com.example.dryclean_admin.service.IProgressStageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 订单服务实现类
 * </p>
 *
 * @author 你的名字
 * @since 2024-01-01
 */
@Service
public class OrderlistServiceImpl extends ServiceImpl<OrderlistMapper, Orderlist> implements IOrderlistService {

    @Autowired
    private IDetergentService detergentService;

    @Autowired
    private IProgressStageService progressStageService;

    @Override
    public Map<String, Object> getOrderProgress(Integer orderId) {
        Orderlist orderlist = this.getById(orderId);
        if (orderlist == null) {
            return null;
        }

        Map<String, Object> result = new HashMap<>();

        // 获取洗涤剂信息
        Detergent detergent = detergentService.getById(orderlist.getDetergentId());
        if (detergent != null) {
            result.put("detergentName", detergent.getName());
            result.put("detergentDescription", detergent.getDescription());
        }

        // 获取进度阶段信息
        ProgressStage progressStage = progressStageService.getById(orderlist.getStageId());
        if (progressStage != null) {
            result.put("stageName", progressStage.getStageName());
            result.put("stageDescription", progressStage.getDescription());
        }

        // 计算当前进度耗时
        if (orderlist.getStartTime() != null) {
            LocalDateTime now = LocalDateTime.now();
            long duration = ChronoUnit.MINUTES.between(orderlist.getStartTime(), now);
            result.put("duration", duration);
        }

        return result;
    }
}
